import type { Schedule, Teacher, Classroom, Course } from '../SchedulingManagementPage';

import React from 'react';
import type { Schedule, Teacher, Classroom } from './ScheduleConflictDetector';

export interface Course {
  id: string;
  name: string;
  description: string;
  credit: number;
}

export class ScheduleExport {
  private schedules: Schedule[];
  private teachers: Teacher[];
  private classrooms: Classroom[];
  private courses: Course[];

  constructor(schedules: Schedule[], teachers: Teacher[], classrooms: Classroom[], courses: Course[]) {
    this.schedules = schedules;
    this.teachers = teachers;
    this.classrooms = classrooms;
    this.courses = courses;
  }

  // 导出为Excel
  exportToExcel(): void {
    // 实际项目中这里应使用如xlsx.js等库来生成Excel文件
    // 简化版：构造CSV内容并下载
    const csvContent = this.generateCSVContent();
    const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
    const link = document.createElement('a');
    const url = URL.createObjectURL(blob);
    
    link.setAttribute('href', url);
    link.setAttribute('download', `排课表_${new Date().toLocaleDateString()}.csv`);
    link.style.visibility = 'hidden';
    
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  }

  // 生成CSV内容
  private generateCSVContent(): string {
    // CSV头部
    const headers = ['课程名称', '教师', '教室', '日期', '开始时间', '结束时间', '学生'];
    let csvContent = headers.join(',') + '\n';
    
    // CSV内容行
    this.schedules.forEach(schedule => {
      const course = this.courses.find(c => c.id === schedule.courseId);
      const teacher = this.teachers.find(t => t.id === schedule.teacherId);
      const classroom = this.classrooms.find(c => c.id === schedule.classroomId);
      
      const startTime = new Date(schedule.startTime);
      const endTime = new Date(schedule.endTime);
      
      // 格式化日期和时间
      const date = `${startTime.getFullYear()}-${(startTime.getMonth() + 1).toString().padStart(2, '0')}-${startTime.getDate().toString().padStart(2, '0')}`;
      const startTimeStr = `${startTime.getHours().toString().padStart(2, '0')}:${startTime.getMinutes().toString().padStart(2, '0')}`;
      const endTimeStr = `${endTime.getHours().toString().padStart(2, '0')}:${endTime.getMinutes().toString().padStart(2, '0')}`;
      
      // 构造CSV行
      const row = [
        course?.name || '',
        teacher?.name || '',
        classroom?.name || '',
        date,
        startTimeStr,
        endTimeStr,
        schedule.studentIds.join(';')
      ];
      
      csvContent += row.map(cell => `"${cell}"`).join(',') + '\n';
    });
    
    return csvContent;
  }

  // 导出为PDF（简化版）
  exportToPDF(): void {
    // 实际项目中这里应使用如jsPDF等库来生成PDF文件
    alert('PDF导出功能待实现');
  }

  // 导出为iCal格式（简化版）
  exportToiCal(): void {
    // 实际项目中这里应生成符合iCal标准的文件
    alert('iCal导出功能待实现');
  }
}

export default ScheduleExport;